package com.tz.farmer.controller;

import cn.hutool.core.util.StrUtil;
import com.smartPark.msframe.result.ResultEx;

import com.tz.farmer.util.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/oss")
@Api(tags = "文件上传" )
@Slf4j
public class OSSManageController {

    @PostMapping("/uploadImg")
    @ApiOperation("上传图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件图片", dataType = "", defaultValue = "", required = false)
    })
    public ResultEx uploadImg(@RequestParam("file") MultipartFile file) {

        try {
            if (file != null) {
                String fileName = file.getOriginalFilename();
                //文件上传到oss，返回文件路径
                String uploadUrl = OssUtils.uploadOssFile(file, "file/" + fileName);
                return ResultEx.builder().makeSuccessResult(uploadUrl);
            } else {
                return ResultEx.builder().makeFailedResult("没有发现文件！");
            }
        } catch (Exception e) {
            log.error("上传失败", e);
            return ResultEx.builder().makeFailedResult("上传失败！");
        }
    }

}

